/*
 * lcd_c.c
 *
 * Created: 10.03.2012 19:25:34
 *  Author: jbolay
 */


#include <avr/io.h>
#include <avr/interrupt.h>
#include "lcd.h"

volatile int counter = 0;
int init = -1;
int even = 0;
int cursor = 0;
int lengt = 0;

int main(void)
{
	//DDRD =	0b00111111;
	//PORTD = 0b00000001;


	lcd_init();

    char data = "Hab dich Lieb!! noch viel mehr";

	printString(data);

    while(1)
    {
		if(((cursor > 0 || cursor + 15 >= lengt) && counter > 8) || (counter > 16)){
			counter = 0;
			++cursor;
			if(cursor + 16 > lengt){
				cursor = 0;
			}
			lcd_string(data+cursor);


			if(even){
				++even;
				lcd_setcursor( 12, 2 );
				lcd_string("hit");
				lcd_setcursor( 0, 1 );
			} else {
				--even;
				lcd_setcursor( 12, 2 );
				lcd_string("   ");
				lcd_setcursor( 0, 1 );
			}
		}
    }
}

int printString(char *data){
	lengt = strlen(data);
	if(lengt > 16){
		lcd_setcursor( 0, 2 );
		lcd_string("strlen: ");
		char str[3];
		lcd_string(itoa(lengt, str, 10));
		if(init){
			init = 0;
			initTimer();

		}
	}
	lcd_setcursor( 0, 1 );
	lcd_string(data);
	lcd_setcursor( 0, 1 );
	return lengt;
}

int initTimer(){
	TCCR0 = (1<<CS02|1<<CS00);
	TIMSK |= (1<<TOIE0);
	sei();
	return 0;
}

ISR (TIMER0_OVF_vect){
	++counter;
}
